﻿using HDSmartCommunity.BUS;
using HDSmartCommunity.DAL;
using HDSmartCommunity.Models;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;

namespace HDSmartCommunity.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class CardInfoController : ControllerBase
    {
        public CardInfoController()
        {
        }

        [HttpPost]
        [Route("GetAllVoucher")]
        public ResponseMessage GetAllVoucher()
        {
            ResponseMessage message = new ResponseMessage();
            CardInfoDAL da = new CardInfoDAL();
            try
            {
                List<VoucherInfo> list = da.GetAllVoucher();
                message.errcode = 0;
                message.data = list;
                message.total = list.Count;
            }
            catch (Exception ex)
            {
                message.errcode = 1;
                message.errmsg = "获取凭证异常";
            }
            return message;
        }

        [HttpPost]
        [Route("GetVoucherByID")]
        public ResponseMessage GetVoucherByID(VoucherInfo info)
        {
            ResponseMessage message = new ResponseMessage();
            CardInfoDAL da = new CardInfoDAL();
            try
            {
                VoucherInfo list = da.GetVoucherByID(info.ID);
                message.errcode = 0;
                message.data = list;
                message.total = 1;
            }
            catch (Exception ex)
            {
                message.errcode = 1;
                message.errmsg = "载入凭证异常";
            }
            return message;
        }

        [HttpPost]
        [Route("UpdateVoucherByID")]
        public ResponseMessage UpdateVoucherByID(VoucherInfo info)
        {
            ResponseMessage message = new ResponseMessage();
            CardInfoDAL da = new CardInfoDAL();
            try
            {
                bool result = da.UpdateVoucherByID(info);
                if (result)
                {
                    message.errcode = 0;
                    message.total = 1;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = "更新凭证失败";
                }
            }
            catch (Exception ex)
            {
                message.errcode = 1;
                message.errmsg = "更新凭证异常";
            }
            return message;
        }

        [HttpPost]
        [Route("DeleteVoucher")]
        public ResponseMessage DeleteVoucher(VoucherInfo info)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                CardBUS bus = new CardBUS();
                bool result = bus.DeleteVoucherInfo(info);

                if (result)
                {
                    message.errcode = 0;
                    message.total = 1;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = "删除凭证失败";
                }
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "删除凭证异常";
            }
            return message;
        }

        [HttpPost]
        [Route("InsertVoucherInfo")]
        public ResponseMessage InsertVoucherInfo(VoucherInfo info)
        {
            ResponseMessage message = new ResponseMessage();

            try
            {
                CardBUS bus = new CardBUS();
                bool result = bus.InsertVoucherInfo(info);

                if (result)
                {
                    message.errcode = 0;
                    message.total = 1;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = "新增凭证失败";
                }
            }
            catch (Exception ex)
            {
                message.errcode = 1;
                message.errmsg = "新增凭证异常";
            }
            return message;
        }

        [HttpPost]
        [Route("QueryVoucherInfo")]
        public ResponseMessage QueryVoucherInfo(VoucherInfo voucher)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                CardBUS bus = new CardBUS();
                List<VoucherInfo> list = bus.QueryVoucherInfo(voucher);
                message.errcode = 0;
                message.data = list;
                message.total = bus.QueryVoucherCount(voucher);
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "查询凭证异常";
            }
            return message;
        }

        [HttpPost]
        [Route("GetFloor")]
        public ResponseMessage GetFloor()
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                CardBUS bus = new CardBUS();
                List<FloorInfo> list = bus.GetFloor();
                message.errcode = 0;
                message.data = list;
                message.total = list.Count;
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "获取楼层异常";
            }
            return message;
        }

        [HttpPost]
        [Route("GetRole")]
        public ResponseMessage GetRole()
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                CardBUS bus = new CardBUS();
                List<Role> list = bus.GetRole();
                message.errcode = 0;
                message.data = list;
                message.total = list.Count;
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "获取权限类型异常";
            }
            return message;
        }

        [HttpPost]
        [Route("GetCredenceType")]
        public ResponseMessage GetCredenceType()
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                CardBUS bus = new CardBUS();
                List<CredenceType> list = bus.GetCredenceType();
                message.errcode = 0;
                message.data = list;
                message.total = list.Count;
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "获取验证类型异常";
            }
            return message;
        }
    }
}
